//
//  SugestionViewController.m
//  QcMedical1
//
//  Created by ios001 on 2016/12/21.
//  Copyright © 2016年 jiawen Du. All rights reserved.
//

#import "SugestionViewController.h"
#import "DescCell.h"

@interface SugestionViewController ()<UITableViewDataSource,UITextViewDelegate>

@property (nonatomic, strong) UITextView *sugestionTextView;

@property (nonatomic, strong) UIView *descView;

@property (nonatomic, strong) UILabel *descLabel;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *imageNames;

@property (nonatomic, strong) NSArray *descTexts;

@property (nonatomic, assign) NSInteger length;

@property(nonatomic,weak) UILabel *countLabel;


@end

static NSString *descCell = @"descCell";

@implementation SugestionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}
- (void)textViewDidChange:(UITextView *)textView{
    
    NSLog(@"%d",(int)textView.text.length);
    int count=(int)textView.text.length;
    self.countLabel.text=[NSString stringWithFormat:@"%d",200-count];
    
}

////加通知
//- (void)addObserver{
//    //开始编辑
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginediting:) name:UITextViewTextDidBeginEditingNotification object:self];
//    
//    //停止编辑
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endediting:) name:UITextViewTextDidEndEditingNotification object:self];
//}
//
//- (void)beginediting:(NSNotification *)notification{
//     
//}
//
//- (void)endediting:(NSNotification *)notification{
//    NSInteger length = _sugestionTextView.attributedText.length;
//    _length = length;
//    self.countLabel.text = @(200 - length);
//}



- (void)setupUI{
    
    self.automaticallyAdjustsScrollViewInsets = NO;

    [self.view addSubview:self.sugestionTextView];
    [self.view addSubview:self.descView];
    [self.view addSubview:self.descLabel];
    [self.view addSubview:self.tableView];
    
    [self.sugestionTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).mas_offset(64+10);
        make.left.equalTo(self.view).mas_offset(10);
        make.right.equalTo(self.view).mas_offset(-10);
        make.height.mas_equalTo(200);
    }];
    [self.descView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sugestionTextView.mas_bottom);
        make.width.equalTo(self.view);
        make.height.mas_equalTo(60);
    }];
    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.descView.mas_bottom).mas_offset(10);
        make.left.equalTo(self.view).mas_offset(15);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.descLabel.mas_bottom).mas_offset(10);
        make.width.equalTo(self.view);
        make.height.mas_equalTo(100);
    }];
}

#pragma mark - dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DescCell *cell = [tableView dequeueReusableCellWithIdentifier:descCell forIndexPath:indexPath];
    cell.imgV.image = [UIImage imageNamed:self.imageNames[indexPath.row]];
    cell.lb_descText.text = self.descTexts[indexPath.row];
    return cell;
}



- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.dataSource = self;
        _tableView.rowHeight = 50;
        [_tableView registerClass:[DescCell class] forCellReuseIdentifier:descCell];
    }
    return _tableView;
}


- (UILabel *)descLabel{
    if (!_descLabel) {
        _descLabel = [[UILabel alloc] init];
        _descLabel.text = @"欢迎联系我们";
        _descLabel.font = [UIFont systemFontOfSize:18];
    }
    return _descLabel;
}

- (UIView *)descView{
    if (!_descView) {
        _descView = [[UIView alloc] init];
        UILabel *textLabel = [[UILabel alloc] init];
        
        textLabel.text = @"您还可以输入的字数为:";
        UILabel *countLabel = [[UILabel alloc] init];
        self.countLabel=countLabel;
        countLabel.text = @"200";
        
        UIButton *loginBtn = [[UIButton alloc] init];
        [loginBtn setBackgroundImage:[UIImage imageNamed:@"dijiaofankui"] forState:UIControlStateNormal];
        [self.descView addSubview:textLabel];
        [self.descView addSubview:countLabel];
        [self.descView addSubview:loginBtn];
        
        [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.descView).mas_offset(80);
            make.width.mas_equalTo(180);
            make.centerY.equalTo(self.descView);
        }];
        [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(textLabel.mas_right);
            make.right.equalTo(loginBtn.mas_left);
            make.centerY.equalTo(self.descView);
        }];
        [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.descView).mas_offset(-10);
            make.top.equalTo(self.descView).mas_offset(10);
            make.bottom.equalTo(self.descView).mas_offset(-10);
            make.width.mas_equalTo(100);
        }];
        
    }
    return _descView;
}

- (UITextView *)sugestionTextView{
    if (!_sugestionTextView) {
        _sugestionTextView = [[UITextView alloc] init];
        
        _sugestionTextView.delegate=self;
        _sugestionTextView.backgroundColor = [UIColor lightGrayColor];
       // _sugestionTextView.text.length
//        _sugestionTextView.text addObserver:self forKeyPath:@"length" options:NSKeyValueObservingOptionNew context:<#(nullable void *)#>
    }
    return _sugestionTextView;
}

- (NSArray *)descTexts{
    if (!_descTexts) {
        _descTexts = @[@"官方电话: 400-636-6113",@"用户交流群: QQ 464768569"];
    }
    return _descTexts;
}

- (NSArray *)imageNames{
    if (!_imageNames) {
        _imageNames = @[@"dianhua",@"qq"];
    }
    return _imageNames;
}


@end
